package com.swap.quartz.job.jobs;


import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.swap.business.domain.SwapDevice;
import com.swap.business.service.DeviceService;
import com.swap.common.exception.BusinessException;
import com.swap.common.utils.DateUtils;
import com.swap.common.utils.StringUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;

import java.util.List;


/**
 * 默认job
 */
public class SwapHandJob implements Job {

    private static final Logger log = LoggerFactory.getLogger(SwapHandJob.class);

    private final RabbitTemplate rabbitTemplate;
    private final DeviceService deviceService;

    public SwapHandJob(RabbitTemplate rabbitTemplate, DeviceService deviceService) {
        this.rabbitTemplate = rabbitTemplate;
        this.deviceService = deviceService;
    }

    @Override
    public void execute(JobExecutionContext jobExecutionContext) {
        try {
            SwapDevice device = new SwapDevice();
            List<SwapDevice> list = deviceService.queryList(device);
            for (SwapDevice div : list) {
                if (StringUtils.isNotEmpty(div.getManufacturer())) {
                    String content = JSON.toJSONString(div);
                    rabbitTemplate.convertAndSend("swap_exchange", "swap.queue", content, message -> {
                        message.getMessageProperties().setContentEncoding("UTF-8");
//                    message.getMessageProperties().setExpiration(String.valueOf(180 * 1000));
//                        message.getMessageProperties().setDeliveryMode(MessageProperties.DEFAULT_DELIVERY_MODE);
                        return message;
                    });
                }
            }
        } catch (BusinessException e) {
            log.error("初始job执行失败：" + e.getMessage());
        } catch (Exception e) {
            log.error("初始job执行失败：" + e.getMessage(), e);
        }
    }
}
